BigDb CRUD
Use BigDb's shortened syntax for simple CRUD operations, run stored queries, and load orms.
Also see: ORMs, Writing Stored SQL
Docs
- CRUD API
- ORM API
- Stored Query API
CRUD API
Execute simple sql statements and get array row results.
-
$db->select('table_name', $where=['status'=>'public']): array
: get an array of rows matching$where
-
$db->insert('table_name', $new_row = [...]): int
: Insert a new row, return the new row's primary int id -
$db->update('table_name', $where = ['id'=>3], $new_values = ['title'=>'New Title']): int
: Update rows matching$where
by setting$new_values
-
$db->delete('article', $where = ['id'=>3]): bool
: Delete rows matching$where
-
$db->sql_query('SELECT * FROM ...', ['status'=>'public']): array
: Get array results from any query.
ORM API
Query the database and return ORM objects for each row. The 'table_name' is both the database table name AND the class name.
For get()
, the ORM class name must be the same as the database table name (unless your BigDb subclass has a special mapping).
-
$db->get('table_name', $where=['status'=>'public']): array
: get an array of Orms -
$db->row_to_orm('table_name', array $row): Tlf\BigOrm
: Get an Orm from an array row -
$item = new \MyNamespace\OrmClass($db); $item->set_from_db($row)
: Alternative method to instantiate an ORM from an array row. -
$orm->get_db_row()
to convert an orm object into a database friendly row array.* - Also see
query()
underStored Query API
below.
Stored Query API
WARNING: The $experimental_binds
use str_replace()
and PDO::quote()
instead of the standard bind params feature in PDO.
$query_name
is something like 'create.book' for a query in create.sql
named book
-
$db->query_rows($query_name, $experimental_binds = []): array
: Get an array of rows by executing a stored query. -
$db->exec($query_name, $experimental_binds = []): int|false
return value is the same asPDO::exec()
-
$db->query('table_name', $query_name, $experimental_binds = []): array
: Get an array ofOrms
by executing a stored query. -
$db->build_query($query_name, $experimental_binds): string
returns a string sql statement
ALSO:
-
$db->recompile_sql()
: Checks if any.sql
files have been updated and recompiles them if yes. (Tip: Use this in migrations files or bin scripts, but not in production) -
$db->addSqlDir($db->root_dir.'/db/sqlv1/', $force_recompile = false)
: Add a directory of stored SQL queries. Use this for migrations to preserve old versions of stored queries.